﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace FranceTriple.Dal
{

    public interface ILogger
    {
        void Log(LoggingCategory category, String message);
        void Clear();
        void Flush();
    }

    public class ConsoleLogger : ILogger
    {        
        #region ILogger Members

        public void  Log(LoggingCategory category, String message)
        {
            switch (category)
            {
                case LoggingCategory.Warning:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                case LoggingCategory.Error:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;
            }

         	Console.WriteLine(category);
            Console.WriteLine(message);           
        }

        public void Clear()
        {
            Console.Clear();
        }

        public void Flush() { }

    #endregion
    }


    public class FileLogger : ILogger
    {
        #region ILogger Members

        public FileLogger()
        {
            Path = "Log.txt";
        }

        public String Path { get; set; }
                
        public void Log(LoggingCategory category, String message)
        {
            try
            {
                var writer = File.AppendText(Path);
                writer.WriteLine(String.Format("Category: {0}, Message: {1}", category.ToString(), message));
                writer.Flush();
            }
            catch (Exception ex)
            {
                //In case the message cannot be written to a file - write it to the EventLog
                EventLog.WriteEntry(Process.GetCurrentProcess().ProcessName,
                    String.Format("CouldNot log the message: {0}, Exception: {1}", message, ex.ToString()),EventLogEntryType.Error);
            }
            
           
        }

        public void Clear()
        {
            File.Delete(Path);
        }

        public void Flush() { }

        #endregion
    }


    public class MemoryLogger : ILogger
    {
        StringBuilder sb = new StringBuilder();
        
        public MemoryLogger()
        {
            //Default value
            Path = "Log.txt";            
        }

        public String Path { get; set; }
        #region ILogger Members

        public void Log(LoggingCategory category, String message)
        {
            sb.AppendLine(String.Format("Category: {0}, Message: {1}", category.ToString(), message));
        }

        public void Clear()
        {
            sb.Clear();
            if (File.Exists(Path))
                File.Delete(Path);
            sb.AppendLine(DateTime.Now.ToLongTimeString());
        }

        public void Flush()
        {            
            File.WriteAllText(Path, sb.ToString());
        }

        #endregion
    }

    public enum LoggingCategory
    {
        Info,
        Warning,
        Error
    }
}
